package com.wyp.didishige.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wyp.didishige.activity.users.UserInfoActivity;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.utils.ToastUtil;
import com.wyp.didishige.wiget.CircleImageView;
import com.wyp.didishige.R;
import com.wyp.didishige.model.CommentV3;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.DateUtil;
import com.wyp.didishige.utils.ImageLoadOptions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.DeleteListener;

/**
 * 评论列表适配器
 * Created by horry on 2015/4/28.
 */
public class CommentAdapter extends BaseAdapter {
    private List<CommentV3> mList;
    private Context mContext;
    AlertDialog.Builder builder;
    public CommentAdapter(Context context, List<CommentV3> list) {
        super();
        this.mList = list;
        this.mContext = context;
        builder=new AlertDialog.Builder(mContext);
    }

    public void appendToList(List<CommentV3> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void add(CommentV3 commentV2){
        mList.add(commentV2);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.comment_item_v2, null);
            holder.user_head = (CircleImageView) convertView.findViewById(R.id.user_head);

            holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
            holder.comment_tag = (TextView) convertView.findViewById(R.id.comment_tag);
            holder.comment_time = (TextView) convertView.findViewById(R.id.comment_time);
            holder.reply_name = (TextView) convertView.findViewById(R.id.reply_name);

            holder.comment = (TextView) convertView.findViewById(R.id.comment);
            holder.delete = convertView.findViewById(R.id.delete);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final CommentV3 commentV3 = mList.get(position);

        final User user = commentV3.getCommentUser();
        final User reply_user = commentV3.getAtUser();

        if (null != reply_user) {
            holder.comment_tag.setText("回复");
            holder.comment_tag.setVisibility(View.VISIBLE);
            holder.reply_name.setVisibility(View.VISIBLE);
            holder.reply_name.setText(reply_user.getNick());
        } else {
            holder.comment_tag.setVisibility(View.GONE);
            holder.reply_name.setVisibility(View.GONE);
        }

        User loginUser = BmobUser.getCurrentUser(mContext,User.class);
        if (loginUser!=null&&user.getObjectId()!=null&&user.getObjectId().equals(loginUser.getObjectId())){
            holder.delete.setVisibility(View.VISIBLE);
        }else{
            holder.delete.setVisibility(View.GONE);
        }
        holder.user_name.setText(user.getNick());

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteThis(commentV3);
            }
        });
        if (user.getAvatar() != null) {
            ImageLoader.getInstance().displayImage(user.getAvatar(), holder.user_head, ImageLoadOptions.getOptions());
            holder.user_head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Config.KEY_USER_ID, user.getObjectId());

                    IntentUtil.start_activity((Activity) mContext, UserInfoActivity.class, bundle);

                }
            });
        } else {
            holder.user_head.setImageResource(R.drawable.ic_launcher);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        2015-04-22 23:54:13
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(commentV3.getCreatedAt());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        holder.comment_time.setText(DateUtil.getDateSpan(calendar.getTimeInMillis() / 1000,
                DateUtil.getTimestamp(new Date())));

        holder.comment.setText(commentV3.getContent());
        return convertView;
    }

    /**
     * 删除评论
     * @param commentV3
     */
    private void deleteComment(final CommentV3 commentV3){
        commentV3.delete(mContext, new DeleteListener() {
            @Override
            public void onSuccess() {
                mList.remove(commentV3);
                notifyDataSetChanged();
            }

            @Override
            public void onFailure(int i, String s) {
                ToastUtil.showShort(mContext,"删除失败，请稍候重试");
            }
        });
    }
    private void deleteThis(final CommentV3 commentV3) {
        builder.setTitle("删除评论");
        builder.setMessage("确定删除此条评论");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteComment(commentV3);
                dialog.cancel();
            }
        });
        builder.show();
    }
    class ViewHolder {
        public CircleImageView user_head;
        public TextView user_name;
        public TextView comment_tag;
        public TextView reply_name;
        public TextView comment_time;
        public TextView comment;
        public View delete;
    }
}
